import zlib
import string
import copy
import struct
import time

def standard(data):
    alphabet = ["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega"]
    latin_alphabet = []
    for letter in string.ascii_lowercase:
        latin_alphabet.append(letter)
    for letter in string.ascii_uppercase:
        latin_alphabet.append(letter)
    alphabet.extend(latin_alphabet)

    star_list = data["stars"]
    planet_list = data["planets"]

    for planet in planet_list:
        sid = planet["sid"]
        try:
            if star_list[sid]["heaviest_planet"] < planet["mval"]*10**planet["mexp"]:
                star_list[sid]["heaviest_planet"] = planet["mval"]*10**planet["mexp"]
        except:
            star_list[sid]["heaviest_planet"] = planet["mval"]*10**planet["mexp"]

    star_list.sort(key=lambda e: e["temperature"])

    for i, star in enumerate(star_list):
        if i<len(alphabet):
            bayer = alphabet[i]
        else:
            bayer = str(i)
        heaviest = 0
        try:
            heaviest = star["heaviest_planet"]
        except:
            pass
        star["name"] = bayer+"-"+str(abs(zlib.adler32(struct.pack("fff", star["x"], star["y"], star["z"]))))+"-"+str(heaviest)      
        print star["name"]
    star_list.sort(key=lambda e: e["sid"])

def cmp_function(test_f, data):
    data2 = copy.deepcopy(data)
    start = time.clock()
    test_f(data2)
    end = time.clock()
    dt1 = end-start

    start = time.clock()
    standard(data)
    end = time.clock()
    dt2 = end-start    
    
    for sid, star in enumerate(data["stars"]):
        if data2["stars"][sid]["name"] != star["name"]:
            raise Exception("star_list compare failed at index "+str(sid)+": expected "+star["name"]+", but user list has "+data2["stars"][sid]["name"])

    print "submitted algorithm time:", dt1
    print "standard algorithm time:", dt2
    return True
